package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * Get_Request类
 * 实现获取请求的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class Get_Request implements AxdrType {

    private byte[] dataCode = null;
    private GetRequestChoice choice = GetRequestChoice._ERR_NONE_SELECTED;
    private GetRequestNormal getRequestNormal = null;
    private GetRequestNormalList getRequestNormalList = null;
    private GetRequestRecord getRequestRecord = null;
    private GetRequestRecordList getRequestRecordList = null;
    private GetRequestNext getRequestNext = null;

    public Get_Request() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setGetRequestNormal(GetRequestNormal getRequestNormal) {
        this.resetChoices();
        this.choice = GetRequestChoice.GET_REQUEST_NORMAL;
        this.getRequestNormal = getRequestNormal;
    }

    public void setGetRequestNormalList(GetRequestNormalList getRequestNormalList) {
        this.resetChoices();
        this.choice = GetRequestChoice.GET_REQUEST_NORMAL_LIST;
        this.getRequestNormalList = getRequestNormalList;
    }

    public void setGetRequestRecord(GetRequestRecord getRequestRecord) {
        this.resetChoices();
        this.choice = GetRequestChoice.GET_REQUEST_RECORD;
        this.getRequestRecord = getRequestRecord;
    }

    public void setGetRequestRecordList(GetRequestRecordList getRequestRecordList) {
        this.resetChoices();
        this.choice = GetRequestChoice.GET_REQUEST_RECORD_LIST;
        this.getRequestRecordList = getRequestRecordList;
    }

    public void setGetRequestNext(GetRequestNext getRequestNext) {
        this.resetChoices();
        this.choice = GetRequestChoice.GET_REQUEST_NEXT;
        this.getRequestNext = getRequestNext;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        com.dl698.asn1.axdr.AxdrEnum choosen = new com.dl698.asn1.axdr.AxdrEnum();
        choosen.setConst();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = GetRequestChoice.fromValue((int) choosen.getValue());

        if (this.choice == GetRequestChoice.GET_REQUEST_NORMAL) {
            this.getRequestNormal = new GetRequestNormal();
            codeLength += this.getRequestNormal.decode(input);
            return codeLength;
        } else if (this.choice == GetRequestChoice.GET_REQUEST_NORMAL_LIST) {
            this.getRequestNormalList = new GetRequestNormalList();
            codeLength += this.getRequestNormalList.decode(input);
            return codeLength;
        } else if (this.choice == GetRequestChoice.GET_REQUEST_RECORD) {
            this.getRequestRecord = new GetRequestRecord();
            codeLength += this.getRequestRecord.decode(input);
            return codeLength;
        } else if (this.choice == GetRequestChoice.GET_REQUEST_RECORD_LIST) {
            this.getRequestRecordList = new GetRequestRecordList();
            codeLength += this.getRequestRecordList.decode(input);
            return codeLength;
        } else if (this.choice == GetRequestChoice.GET_REQUEST_NEXT) {
            this.getRequestNext = new GetRequestNext();
            codeLength += this.getRequestNext.decode(input);
            return codeLength;
        }
        throw new RuntimeException("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            try {
                for (int i = this.dataCode.length - 1; i >= 0; i--) {
                    output.write(this.dataCode[i]);
                }
            } catch (java.io.IOException e) {
                throw new RuntimeException("Error writing dataCode to output stream", e);
            }
            return this.dataCode.length;
        }
        if (this.choice == GetRequestChoice._ERR_NONE_SELECTED) {
            throw new RuntimeException("Error encoding AxdrChoice: No item in choice was selected.");
        }

        int codeLength = 0;

        if (this.choice == GetRequestChoice.GET_REQUEST_NEXT) {
            if (this.getRequestNext != null) {
                codeLength += this.getRequestNext.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(4);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("getRequestNext is null");
            }
        } else if (this.choice == GetRequestChoice.GET_REQUEST_RECORD_LIST) {
            if (this.getRequestRecordList != null) {
                codeLength += this.getRequestRecordList.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(3);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("getRequestRecordList is null");
            }
        } else if (this.choice == GetRequestChoice.GET_REQUEST_RECORD) {
            if (this.getRequestRecord != null) {
                codeLength += this.getRequestRecord.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("getRequestRecord is null");
            }
        } else if (this.choice == GetRequestChoice.GET_REQUEST_NORMAL_LIST) {
            if (this.getRequestNormalList != null) {
                codeLength += this.getRequestNormalList.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("getRequestNormalList is null");
            }
        } else if (this.choice == GetRequestChoice.GET_REQUEST_NORMAL) {
            codeLength += this.getRequestNormal.encode(output);
            com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
            c.setMinMaxVal(0);
            codeLength += c.encode(output);
            return codeLength;
        }

        throw new RuntimeException("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding Get_Request", e);
        }
        this.dataCode = revOStream.getArray();
    }

    public GetRequestChoice getChoiceIndex() {
        return this.choice;
    }

    public GetRequestNormal getGetRequestNormal() {
        return this.getRequestNormal;
    }

    public GetRequestNormalList getGetRequestNormalList() {
        return this.getRequestNormalList;
    }

    public GetRequestRecord getGetRequestRecord() {
        return this.getRequestRecord;
    }

    public GetRequestRecordList getGetRequestRecordList() {
        return this.getRequestRecordList;
    }

    public GetRequestNext getGetRequestNext() {
        return this.getRequestNext;
    }

    public void resetChoices() {
        this.choice = GetRequestChoice._ERR_NONE_SELECTED;
        this.getRequestNormal = null;
        this.getRequestNormalList = null;
        this.getRequestRecord = null;
        this.getRequestRecordList = null;
        this.getRequestNext = null;
    }

    @Override
    public String toString() {
        if (this.choice == GetRequestChoice.GET_REQUEST_NORMAL) {
            if (this.getRequestNormal != null) {
                return "choice: {get_request_normal: " + this.getRequestNormal + "}";
            } else {
                return "choice is get_request_normal but getRequestNormal is null";
            }
        } else if (this.choice == GetRequestChoice.GET_REQUEST_NORMAL_LIST) {
            if (this.getRequestNormalList != null) {
                return "choice: {get_request_normal_list: " + this.getRequestNormalList + "}";
            } else {
                return "choice is get_request_normal_list but getRequestNormalList is null";
            }
        } else if (this.choice == GetRequestChoice.GET_REQUEST_RECORD) {
            if (this.getRequestRecord != null) {
                return "choice: {get_request_record: " + this.getRequestRecord + "}";
            } else {
                return "choice is get_request_record but getRequestRecord is null";
            }
        } else if (this.choice == GetRequestChoice.GET_REQUEST_RECORD_LIST) {
            if (this.getRequestRecordList != null) {
                return "choice: {get_request_record_list: " + this.getRequestRecordList + "}";
            } else {
                return "choice is get_request_record_list but getRequestRecordList is null";
            }
        } else if (this.choice == GetRequestChoice.GET_REQUEST_NEXT) {
            if (this.getRequestNext != null) {
                return "choice: {get_request_next: " + this.getRequestNext + "}";
            } else {
                return "choice is get_request_next but getRequestNext is null";
            }
        } else {
            return "unknown";
        }
    }
}